$(function () {
    //加载课程目录
    loadCatalog();

    /**
     * 加载课程目录/套餐目录
     */
    function loadCatalog() {
        var courseId = $("#courseId").val();
        var courseType = $("#courseType").val();
            var  buyCourse =       $("#buyCourse").val();
        $.ajax({
            url:baseloaction +'/web/course/findCourseCatalog',
            type:'post',
            dataType:'html',
            data:{
                'courseId':courseId,
                'courseType':courseType,
                'buyCourse':buyCourse
            },
            success:function (result) {
                $("#course-catalog-box").html(result);
                lazyCourseImage();
                var contextStatus = $("#contextStatus").val();
                if(contextStatus==1){//如果课程未完成更新
                    setTimeout(function () {
                        var courseIds = '';
                        if(courseType =='PACKAGE'){//如果是套餐
                            var courseList = $("div>.course-list-item");
                            if(courseList!=null && courseList.length > 0){
                                courseIds = courseList.map(function () {
                                    return $(this).data('course');
                                }).get().join(",");
                            }
                        }else{//如果不是套餐
                            courseIds = courseId;
                        }
                        if(courseIds!=''){
                            getNewCatalog(courseIds,courseType);
                        }
                    },300);
                }
            }
        });
    }

    /**
     * 获取最新的目录
     * @param courseIds 课程ID串
     * @param courseType 课程类型
     */
    function getNewCatalog(courseIds,courseType) {
        $.ajax({
            url: baseloaction +'/web/course/getNewCatalog',
            type:'post',
            dataType:'json',
            data:{
                'courseIds':courseIds
            },
            success:function (result) {
                if(result.success==true){
                    var obj = result.entity;
                    if(obj !=null){
                        var context = '';
                        if(courseType =='PACKAGE') {//如果是套餐
                            context += obj.course.courseName+'-';
                        }
                        var parentCatalog = obj.parentCatalog;
                        if(parentCatalog != null){
                            context+= parentCatalog.catalogName + "-";
                        }
                        context+= obj.catalogName;
                        $("#last-update").text(context);
                    }
                }
            },
            error:function (error) {

            }
        });
    }
});